package IBBB.view;

import IBBB.model.UserMessage;
import IBBB.model.WordProperty;
import IBBB.model.WordsMessage;
import javafx.beans.binding.StringBinding;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import java.io.IOException;
import IBBB.network.sendmessage.send_ask_for_word;
import IBBB.network.sendmessage.send_update_user_schedule;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class LearnPane {
    @FXML
    private Button exitButton;
    @FXML
    private WebView presentWord;
    @FXML
    private WebEngine engine;
    @FXML
    private Button knowButton;
    @FXML
    private Button unknowButton;
    private WordsMessage wordsMessage;
    private StackPane container;
    private String wordStockName;
    private UserMessage userMessage;
    private boolean isReview;
    @FXML
    private void initialize() {
        engine = presentWord.getEngine();
        knowButton.setDisable(true);
        unknowButton.setDisable(true);
        exitButton.setDisable(true);
        engine.loadContent("<html><body><br><br><div><h1 style=\"text-align:center\">加载中，请耐心等候~</h1></div></body></html>");
    }

    private void warn(String s, String t) {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("WarningPane.fxml"));
        try {
            Parent parent = loader.load();
            WarningPane wp = (WarningPane)loader.getController();
            wp.setMessage(s);
            Stage w = new Stage(StageStyle.UTILITY);
            w.setTitle(t);
            w.setScene(new Scene(parent, 350,220));
            w.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void init(StackPane caller, UserMessage userMessage, String name, boolean isReview) {
        this.isReview = isReview;
        wordStockName = name;
        this.userMessage = userMessage;
        container = caller;
        send_ask_for_word ask_for_word;
        if (isReview) {
            ask_for_word = new send_ask_for_word("localhost", userMessage.getUserName().get(), wordStockName, "Review", userMessage.getPlan(wordStockName));
        } else {
            ask_for_word = new send_ask_for_word("localhost", userMessage.getUserName().get(), wordStockName, "Study", userMessage.getPlan(wordStockName));
        }

        Thread th = new Thread(ask_for_word);
        ask_for_word.messageProperty().addListener(((observableValue, s, t1) -> {
            switch (t1) {
                case "timeout": {
                    warn("网络异常", "警告");
                    container.getChildren().remove(1);
                    container.getChildren().get(0).setVisible(true);
                    break;
                }
                case "wrong": {
                    warn("出现异常", "警告");
                    container.getChildren().remove(1);
                    container.getChildren().get(0).setVisible(true);
                    break;
                }
                case "success": {
                    wordsMessage = ask_for_word.getWordsMessage();
                    WordProperty word = wordsMessage.getWordProperty();
                    StringBinding html = word.hiddenHtmlProperty();
                    html.addListener((observableValue1, s1, t11) -> engine.loadContent(t11));
                    engine.loadContent(word.hiddenHtmlProperty().get());
                    knowButton.setDisable(false);
                    unknowButton.setDisable(false);
                    exitButton.setDisable(false);
                    break;
                }
            }
        }));
        th.start();
        //System.out.println(words.get("around").getPhone());
    }

    private boolean actualFinish = false;
    @FXML
    private void setKnowButtonAction() {
        if (wordsMessage.isFinished()) {
            actualFinish = true;
            engine.loadContent("<html><body><br><br><div style=\"text-align:center\"><h1>本次任务完成啦~</h1></div></body></html>");
            knowButton.setDisable(true);
            unknowButton.setDisable(true);
        } else {
            wordsMessage.nextWord();
        }
    }

    @FXML
    private void setExitButtonAction() {
        exitButton.setDisable(true);
        if(!isReview) {
            send_update_user_schedule update;
            if (actualFinish) {
                update = new send_update_user_schedule("localhost", wordStockName, userMessage.getUserName().get(), wordsMessage.getCount());//TODO
            } else {
                if (wordsMessage.getCount() - 1 != 0)
                    update = new send_update_user_schedule("localhost", wordStockName, userMessage.getUserName().get(), wordsMessage.getCount() - 1);
                else {
                    container.getChildren().remove(1);
                    container.getChildren().get(0).setVisible(true);
                    return;
                }
            }

            Thread t = new Thread(update);
            update.messageProperty().addListener(((observableValue, s, t1) -> {
                switch (t1) {
                    case "timeout": {
                        warn("网络异常", "警告");
                        container.getChildren().remove(1);
                        container.getChildren().get(0).setVisible(true);
                        break;
                    }

                    case "wrong": {
                        warn("出现异常", "警告");
                        container.getChildren().remove(1);
                        container.getChildren().get(0).setVisible(true);
                        break;
                    }
                    case "success": {
                        if (actualFinish) {
                            userMessage.updateSchedule(wordStockName, wordsMessage.getCount());
                        } else {
                            userMessage.updateSchedule(wordStockName, wordsMessage.getCount() - 1);
                        }
                        container.getChildren().remove(1);
                        container.getChildren().get(0).setVisible(true);
                    }
                }
            }));
            t.start();
        } else {
            container.getChildren().remove(1);
            container.getChildren().get(0).setVisible(true);
        }
    }

    @FXML
    private void setUnknowButton() {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("CompleteWordPane.fxml"));
        Parent node = null;
        try {
            node = loader.load();
            CompleteWordPane controller = loader.getController();
            controller.init(wordsMessage, userMessage, container, isReview, wordStockName);
            container.getChildren().get(container.getChildren().size() - 1).setVisible(false);
            container.getChildren().add(node);
        } catch (IOException e) {
            // TODO: handle error
            e.printStackTrace();
        }
    }
}
